/*
	URL: $HeadURL: http://screwplugs.googlecode.com/svn/trunk/src/ScrewPlugs/PathElements.cs $
	Revision: $Revision: 3 $
	Last modified: $Date: 2010-01-03 01:29:03 +0000 (Sun, 03 Jan 2010) $
	Last changed by: $LastChangedBy: slav.isv $
	
	Author: ornus
	Created on: 2009-12-31 20:31:57
	
	Copyright (c) Soltide, LLC
	This file is part of the ScrewPlugs project
*/
using System;
using System.Reflection;
using ScrewTurn.Wiki.PluginFramework;

namespace ScrewPlugs
{
	/// <summary>
	/// Adds support for paths elements in page reference.
	/// </summary>
	/// <remarks>This plug-in processes path elements in wiki links:
	/// * / gets replaced with -
	/// * . refers to the current page
	/// * .. refers to the parent
	/// <para>If current page is Page1:
	/// * ./Page2 = Page1-Page2
	/// * /Page2 = Page1-Page2
	/// * ../Page2 = Page2
	/// * Page2/Page3-Page4/../Page5 = Page2-Page3-Page5
	/// * Page2/./././././Page4 = Page2-Page4</para></remarks>
	public class PathElements : IFormatterProviderV30
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="PathElements"/> class.
		/// </summary>
		public PathElements()
		{
			var typeName = this.GetType().Name;
			var assemblyName = Assembly.GetAssembly( this.GetType() ).GetName();
			var assemblyVersion = assemblyName.Version;
			var version = assemblyVersion.Major + "." + assemblyVersion.Minor + "." + assemblyVersion.Build + "." + assemblyVersion.Revision;
			_information = new ComponentInformation( typeName, "Soltide, LLC", version, "http://code.google.com/p/screwplugs/", null );
			_pathHandler = new PathHandler();
		}

		/// <summary>
		/// Formats the specified raw.
		/// </summary>
		/// <param name="raw">The raw.</param>
		/// <param name="context">The context.</param>
		/// <param name="phase">The phase.</param>
		/// <returns></returns>
		public string Format( string raw, ContextInformation context, FormattingPhase phase )
		{
			try
			{
				if( context != null && context.Page != null && context.Page.FullName != null )
				{
					var format = this._pathHandler.EvaluatePath( raw, NameTools.GetLocalName( context.Page.FullName ));
					//					this._host.LogEntry( "new full text = " + format, LogEntryType.General, null, this );
					return format;
				}
				else
				{
					return raw;
				}
			}
			catch( Exception exc )
			{
				this._host.LogEntry( "An exception was encountered in PathElementPlug-in:\n\n" + exc.Message + "\n\n" +
				                     exc.StackTrace, LogEntryType.Error, null, this );
				return raw;
			}
		}

		public string PrepareTitle( string title, ContextInformation context )
		{
			return title.Replace( "-", " - " );
		}

		/// <summary>
		/// Gets a value indicating whether to format on phase1.
		/// </summary>
		/// <value><c>true</c> if format on phase1; otherwise, <c>false</c>.</value>
		public bool PerformPhase1
		{
			get
			{
				return true;
			}
		}
		
		/// <summary>
		/// Gets a value indicating whether to format on phase2.
		/// </summary>
		/// <value><c>true</c> if format on phase2; otherwise, <c>false</c>.</value>
		public bool PerformPhase2
		{
			get
			{
				return false;
			}
		}
		
		/// <summary>
		/// Gets a value indicating whether to format on phase3.
		/// </summary>
		/// <value><c>true</c> if format on phase3; otherwise, <c>false</c>.</value>
		public bool PerformPhase3
		{
			get
			{
				return false;
			}
		}

		public int ExecutionPriority
		{
			get { return 50; }
		}

		public void Init( IHostV30 host, string config )
		{
			this._host = host;
			
			if( !string.IsNullOrEmpty( config ) && config.Length > 0 )
			{
				this._pathHandler = new PathHandler( config[ 0 ] );
			}
		}

		public void Shutdown( )
		{
		}

		/// <summary>
		/// Gets the information.
		/// </summary>
		/// <value>The information.</value>
		public ComponentInformation Information
		{
			get
			{
				return this._information;
			}
		}

		public string ConfigHelpHtml
		{
			get { return "No config needed."; }
		}

		private readonly ComponentInformation _information;
		private IHostV30 _host;
		private PathHandler _pathHandler;
	}
}
